import 'package:city_pickers/city_pickers.dart';
import 'package:city_pickers/meta/province.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:concierge/generated/locales.g.dart';

class CityModal {
  static String getLocationCodeByCity(String cityName) {
    // 遍历省份
    for (var provinceCode in citiesData.keys) {
      final provinceData = citiesData[provinceCode] as Map<String, dynamic>;
      // 遍历城市
      for (var cityCode in provinceData.keys) {
        final cityData = provinceData[cityCode] as Map<String, dynamic>;
        if (cityData['name'] == cityName) {
          return cityCode;
        }
      }
    }
    return '110000';
  }

  static Future<Result?> show(String city) async {
    final context = Get.context!;
    final cityName = city.split(' ').last;
    final locationCode = getLocationCodeByCity(cityName);
    final result = await CityPickers.showCityPicker(
      context: context,
      showType: ShowType.pc,
      locationCode: locationCode,
      height: 400.w,
      cancelWidget: Text(LocaleKeys.cancel.tr),
      confirmWidget: Text(LocaleKeys.confirm.tr),
    );
    return result;
  }
}
